import { getPreAcceptedBillListApi, getPreAcceptedPageStaticApi } from "@/api/financialManagement/payableManagement";
import CommonHook from "@/hook/commonHook";
import { message } from "ant-design-vue";
import { computed, Ref } from "vue";

export default function OperationHook(state: {
  activeKey: Ref<string>;
  searchForm: StatementPayable.PreAcceptedBill;
  tableData: Ref<StatementPayable.PreAcceptedBillListRow[]>;
  loading: Ref<boolean>;
  tableRef: Ref<any>;
  pageStat: Ref<Base.GeneraDataType>;
  dialogOpts: Base.DialogOpts;
  drawerOpts: Base.DrawerOpts;
}) {
  const { activeKey, searchForm, tableData, tableRef, loading, pageStat, dialogOpts, drawerOpts } = state;
  const selectRows = computed<Array<StatementPayable.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  const { getNoZeroKey } = CommonHook();
  const getData = async () => {
    const params = {
      ...searchForm,
      statusList: [Number(activeKey.value)]
    };
    loading.value = true;
    tableData.value = [];
    return getPreAcceptedBillListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.beforePageNum = searchForm.pageNum;
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const pageStateic = () => {
    const params = {
      ...searchForm,
      statusList: [Number(activeKey.value)]
    };
    return getPreAcceptedPageStaticApi({ ...params }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        Object.assign(pageStat.value, {
          1: data[1],
          2: data[2]
        });
        activeKey.value = String(getNoZeroKey(pageStat.value, ["1", "2"]));
      }
    });
  };
  const searchHandle = async () => {
    searchForm.pageNum = 1;
    await pageStateic();
    getData();
  };
  const changeTabHandle = () => {
    getData();
  };
  // 批量接收账单
  const batchReceivePayableStatementHandle = () => {
    if (!selectRows.value.length) return message.error("请先勾选对账单号!");
    dialogOpts.title = "接收账单";
    dialogOpts.width = "520px";
    dialogOpts.component = "ChooseStatementType";
    dialogOpts.type = 1;
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };
  // 接收账单
  const receivePayableStatementHandle = (row: StatementPayable.PreAcceptedBillListRow) => {
    dialogOpts.title = "接收账单";
    dialogOpts.width = "520px";
    dialogOpts.component = "ChooseStatementType";
    dialogOpts.type = 1;
    dialogOpts.passData = [row];
    dialogOpts.visible = true;
  };
  // 详情
  const showDetailsHandle = (row: StatementPayable.PreAcceptedBillListRow) => {
    drawerOpts.title = `详情`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 2;
    drawerOpts.component = "Details";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };
  return {
    searchHandle,
    changeTabHandle,
    getData,
    selectRows,
    receivePayableStatementHandle,
    batchReceivePayableStatementHandle,
    showDetailsHandle
  };
}
